package com.ld.common.util;

import com.ld.common.util.security.ProjectSecurityUtil;
import com.ld.shieldsb.common.core.encryptor.BlowfishEncryptor;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.common.core.util.pwdchecker.PwdChecker;

public class PassUtil {

    // 密钥，建议根据不同的业务添加不同的密钥，防止一个密钥泄露造成的数据不安全，
    // 需要调用ProjectSecurityUtil.load(ProjectSecurityKey model)加载加密类，ProjectSecurityKey在主项目中
    public static final String PASS_KEY = ProjectSecurityUtil.getSecurityKey().getPassKey();
    public static final BlowfishEncryptor BFISH = new BlowfishEncryptor(PASS_KEY);
    private static final PwdChecker cherker = ProjectSecurityUtil.getSecurityKey().getPwdChecker();

    /**
     * 加密
     * 
     * @param pass
     * @return
     */
    public static String encryptPass(String pass) {
        return BFISH.encrypt(pass);
    }

    /**
     * 解密
     * 
     * @param pass
     * @return
     */
    public static String decryptPass(String pass) {
        return BFISH.decrypt(pass);
    }

    public static void main(String[] args) {
        String pass = encryptPass("123456");
        System.out.println(pass + " " + decryptPass(pass));
        System.out.println(" " + checkIsSafe("123456"));
        String pw1 = "ABCDEFGHIG";
        String pw2 = "abcdefghig";
        String pw3 = "0123456789";
        String pw4 = "!@#$%^&*()";
        String pw5 = "ABCDEabcde";
        String pw6 = "ABCDE01234";
        String pw7 = "ABCDE!@#$%";
        String pw8 = "abcde01234";
        String pw9 = "abcde!@#$%";
        String pw10 = "01234!@#$%";
        String pw11 = "abcde01234!@#$%";
        String pw12 = "ABCDE01234!@#$%";
        String pw13 = "ABCDEabcde!@#$%";
        String pw14 = "ABCDEabcde01234";
        String pw15 = "Aa0!";
        // 符合要求密码
        String pw16 = "ABCabc012!";
        System.out.println(pw1 + " " + checkIsSafe(pw1));
        System.out.println(pw2 + " " + checkIsSafe(pw2));
        System.out.println(pw3 + " " + checkIsSafe(pw3));
        System.out.println(pw4 + " " + checkIsSafe(pw4));
        System.out.println(pw5 + " " + checkIsSafe(pw5));
        System.out.println(pw6 + " " + checkIsSafe(pw6));
        System.out.println(pw7 + " " + checkIsSafe(pw7));
        System.out.println(pw8 + " " + checkIsSafe(pw8));
        System.out.println(pw9 + " " + checkIsSafe(pw9));
        System.out.println(pw10 + " " + checkIsSafe(pw10));
        System.out.println(pw11 + " " + checkIsSafe(pw11));
        System.out.println(pw12 + " " + checkIsSafe(pw12));
        System.out.println(pw13 + " " + checkIsSafe(pw13));
        System.out.println(pw14 + " " + checkIsSafe(pw14));
        System.out.println(pw15 + " " + checkIsSafe(pw15));
        System.out.println(pw16 + " " + checkIsSafe(pw16));

    }

    /**
     * 检验密码是否过于简单
     * 
     * @Title checkIsSafe
     * @author 吕凯
     * @date 2016年4月12日 下午2:52:39
     * @param newpwd
     * @return boolean
     */
    public static Result checkIsSafe(String newpwd) {
        return cherker.checkIsSafe(newpwd);
    }

}
